package com.example.ice.u17;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ice.u17.adapter.ChapterAdapter;
import com.example.ice.u17.entries.level1.DatailBean;
import com.example.ice.u17.entries.level1.DetailGuessBean;
import com.example.ice.u17.util.ClassType;
import com.example.ice.u17.util.NetUtils;
import com.example.ice.u17.widget.MyPopupWindow;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChapterActivity extends AppCompatActivity implements Callback<DatailBean>, View.OnTouchListener {

    private ImageView mChapterBack;
    private ImageView mChapterDownload;
    private RecyclerView mRecyclerView;
    private List<Object> list;
    private int ids[];
    private ChapterAdapter mchapterAdapter;
    private LinearLayout preview;
    private LinearLayout mChapterBottom;
    private String comicid;
    private TextView collection;
    private TextView read;
    private TextView page;
    private int read_page;
    private boolean isRead;
    private SharedPreferences read_history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter);

        Intent intent = getIntent();
        comicid = intent.getStringExtra("comicid");
        read_history = getSharedPreferences(comicid, MODE_PRIVATE);
        read_page = read_history.getInt("page", -1);
        isRead = read_history.getBoolean(comicid,false);
        mChapterBack = ((ImageView) findViewById(R.id.chapter_back));
        mChapterDownload = ((ImageView) findViewById(R.id.chapter_download));
        collection = ((TextView) findViewById(R.id.collection));
        read = ((TextView) findViewById(R.id.read));
        mRecyclerView = ((RecyclerView) findViewById(R.id.chapter_recycler));
        preview = ((LinearLayout) findViewById(R.id.preview));
        mChapterBottom = ((LinearLayout) findViewById(R.id.chapter_bottom));
        page = ((TextView) findViewById(R.id.page));
        if(read_page == -1){
            page.setText("暂未阅读");
        }else{
            page.setText("上次阅读到"+read_page+"话");
        }
        if (isRead) {
            read.setText("继续阅读");
        }
        list = new ArrayList<>();
        ids = new int[]{R.layout.chapter_head,R.layout.chapter_list,R.layout.chapter_guess};
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mchapterAdapter = new ChapterAdapter(ChapterActivity.this, list, ids,comicid);
        mRecyclerView.setAdapter(mchapterAdapter);
        mRecyclerView.setOnTouchListener(this);
        NetUtils.getData(ClassType.DatailBean,this,comicid);
    }
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.chapter_back:
                finish();
                break;
            case R.id.chapter_download:

                break;
            case R.id.collection:
                MyPopupWindow myPopupWindow = new MyPopupWindow(this);
                myPopupWindow.showAtLocation(findViewById(R.id.activity_chapter), Gravity.BOTTOM, 0, 0);
                break;
            case R.id.read:
                Intent intent = new Intent(this, ReadComicActivity.class);
                if (isRead) {
                    int position = read_history.getInt("position", -1);
                    intent.putExtra("showposition",position);
                }else{
                    intent.putExtra("showposition", 0);
                }
                intent.putExtra("comicid", comicid);
                startActivity(intent);
                break;
        }
    }
    @Override
    public void onResponse(Call<DatailBean> call, Response<DatailBean> response) {
        DatailBean detailBean = response.body();

        mchapterAdapter.add(0, detailBean.getData().getReturnData().getComic());
        mchapterAdapter.add(1, detailBean.getData().getReturnData().getChapter_list());
        NetUtils.getData(ClassType.DetailGuessBean, new Callback<DetailGuessBean>() {
            @Override
            public void onResponse(Call<DetailGuessBean> call, Response<DetailGuessBean> response) {
                DetailGuessBean detailGuessBean = response.body();
                preview.setVisibility(View.GONE);
                mchapterAdapter.add(2, detailGuessBean.getData().getReturnData());
            }

            @Override
            public void onFailure(Call<DetailGuessBean> call, Throwable t) {
                Toast.makeText(ChapterActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onFailure(Call<DatailBean> call, Throwable t) {
        Toast.makeText(this, "数据请求失败", Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Animation out_animation = AnimationUtils.loadAnimation(this, R.anim.chapter_out_anim);
                out_animation.setFillAfter(true);
                mChapterBottom.startAnimation(out_animation);
                break;
            case MotionEvent.ACTION_UP:
                Animation in_animation = AnimationUtils.loadAnimation(this, R.anim.chapter_in_anim);
                in_animation.setFillAfter(true);
                mChapterBottom.startAnimation(in_animation);
                break;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        list.clear();
        list = null;
    }
}
